﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dbPoll_Application.Models.Poll>" %>
<%@ Import Namespace="dbPoll_Application.Helpers" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Polls
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="../../Scripts/Poll-Questions.js"></script>

    <div class="pageHeadingContainer"><h2 class="pageHeading">Poll Details</h2></div>
    <div id="contentContainer">
    <p><%= Html.ActionLink("Polls > ", "Index", "Poll", null, new { @class = "breadcrumb" })%><% if (Model.pollID < 0)
                                                                                                 { %>New Poll <% } %><%= Model.name %></p>
    <div id="pageMenuContainer">
        <% var pollRoles = ViewData["pollRoles"] as ICollection<dbPoll_Application.Models.Role>;
           var pollAdministrator = new dbPoll_Application.Models.Role("Poll Administrator");
           var pollCreator = new dbPoll_Application.Models.Role("Poll Creator");
           var pollMaster = new dbPoll_Application.Models.Role("Poll Master"); %>
        <ul id="pageMenu">
            <li>Details</li>
            <li id="runLink" <% if (Model.pollID < 0 || Model.questions.Count == 0 || Model.pollClose<DateTime.Now ) { %> class="none" <% } %>><%= Html.ActionLink("Run", "SetSession", new { pollID = Model.pollID }) %></li>
            <li id="testLink" <% if (Model.pollID < 0 || Model.questions.Count == 0  || Model.pollClose<DateTime.Now) { %> class="none" <% } %>><%= Html.ActionLink("Test", "Test", new { pollID = Model.pollID }) %></li>
            <li id="reportsLink" <% if (Model.pollID < 0 || Model.questions.Count == 0) { %> class="none" <% } %>><%= Html.ActionLink("Reports", "Reports", new { pollID = Model.pollID })%></li>
            <li id="hierachyLink" <% if (Model.pollID < 0) { %> class="none" <% } %>><%= Html.ActionLink("Manage Hierarchy", "EntityHierarchy", new { pollID = Model.pollID }) %></li>
        </ul>
    </div>
    <div id="questionsContainer">
        <input type="hidden" id="questionCount" value="<%= Model.questions.Count %>" />
        <% String actionName;
           if (Model.pollID < 0)
           {
               actionName = "AddPoll";
           }
           else
           {
               actionName = "UpdatePoll";
           } %>
        <div>
                <table>
                    <tr><td><input type="hidden" value="<%= actionName %>" name="actionName" id="actionName" /></td><td></td><td></td></tr>
                    <tr><td><input type="hidden" value="<%= Model.pollID %>" name="main_pollID" id="main_pollID" /></td><td></td><td></td></tr>
                    <tr><td><label for="name">Poll Name:</label></td><td><%= Html.TextBoxFor(m => m.name)%></td></tr>
                    <tr><td><label for="receiverChannel">Receiver Channel:</label></td><td><%= Html.DropDownListFor(m => m.receiverChannel, new SelectList(Enumerable.Range(1, 82)))%></td></tr>
                    <tr><td><label for="genericUsers">Tracked:</label></td><td><%= Html.CheckBoxFor(m => m.genericUsers) %></td></tr>
                    <tr><td><label for="hasParticipantList">Participant List:</label></td><td><%= Html.CheckBoxFor(m => m.hasParticipantList)%>
                    <% if (Model.hasParticipantList)
                       { %>
                       <span id="participant"><%= Html.ActionLink("[edit]", "Participants", new { pollID = Model.pollID, pollName = Model.name }, new { id = "partname" })%></span>
                    <% }
                       else
                       { %>
                       <span id="participant" class="none"><%= Html.ActionLink("[edit]", "Participants", new { pollID = Model.pollID, pollName = Model.name }, new {id="partname"})%></span><span class="error" id="parterrormessage"></span>
                    <% } %>
                    </td></tr>
                    <tr><td>Participants: </td><td><a href="#" id="list"><%= Model.participants.Count.ToString()%></a><span class="error" id="listerrormessage"></span></td></tr>
                </table>
                
        </div>
    <div class="questionsContainerHeadings"><h2>Creators</h2></div>
    <div><table class="questions" id="creators">
        <tr>
            <th class="selectUserHeader">User</th>
            <th class="selectUserHeader">Firstname</th>
            <th class="selectUserHeader">Lastname</th>
            <th class="selectUserHeader">Organisation</th>
            <th class="selectUserHeader">City</th>
            <th></th>
        </tr>
        <% foreach (var creator in ViewData["pollCreators"] as IList<dbPoll_Application.Models.User>)
           { %>
           <tr>
               <td><%= creator.UserName %></td>
               <td><%= creator.FirstName %></td>
               <td><%= creator.LastName%></td>
               <td><%= creator.Organisation%></td>
               <td><%= creator.City%></td>
               <td><% if (pollRoles.Contains(pollAdministrator))
                      { %><input type="button" value="Remove" class="removeCreator" /> <% } %></td>
           </tr>
        <% } %>
    </table>
        
    <% if (pollRoles.Contains(pollAdministrator)) { %><input type="submit" value="Add Creator" id="addCreator" /></a><span class="error" id="addCreatorErrorMessage"></span> <% } %></div>
    <div class="questionsContainerHeadings"><h2>Masters</h2></div>
    <div><table class="questions" id="masters">
        <tr>
            <th class="selectUserHeader">User</th>
            <th class="selectUserHeader">Firstname</th>
            <th class="selectUserHeader">Lastname</th>
            <th class="selectUserHeader">Organisation</th>
            <th class="selectUserHeader">City</th>
            <th></th>
        </tr>
        <% foreach (var master in Model.pollMasters)
           { %>
           <tr>
               <td><%= master.UserName %></td>
               <td><%= master.FirstName %></td>
               <td><%= master.LastName%></td>
               <td><%= master.Organisation%></td>
               <td><%= master.City%></td>
               <td><% if (pollRoles.Contains(pollCreator))
                      { %><input type="button" value="Remove" class="removeMaster" /> <% } %></td>
           </tr>
        <% } %>
    </table>
    <% if (pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator))
       { %><input type="submit" value="Add Master" id="addMaster" /><span class="error" id="addMasterErrorMessage"></span> <% } %></div>
        <div class="questionsContainerHeadings"><h2>Test Questions</h2></div>
    <table class="questions">
    <thead>
    <tr>
    <th class="questionNumberCol"></th>
    <th class="questionCol">Question</th>
    <th class="questionTypeCol">Type</th>
    <th class="questionDelCol"></th>
    </tr>
    </thead>
    <% int nextTestQuestionNumber = 1;
       foreach (var question in Model.linkedPoll.questions)
       {
           if (question.deletionTime == null)
           {
               nextTestQuestionNumber++; %>
    <tr id="Tr1" class="pollDetailsQuestionRow">
    <td><%= question.questionNumber%></td>
    <td class="questionCol"><%= Html.ActionLink(question.question.CropAndTrail(45, "..."), "Edit" + question.type, "Question", new { questionID = question.questionID, questionNumber = question.questionNumber, pollID = Model.linkedPoll.pollID, pollName = "Test Poll" }, null)%></td>
    <td class="questionTypeCol"><%= (question.type == dbPoll_Application.Models.QuestionType.Alphanumeric || question.type == dbPoll_Application.Models.QuestionType.Numeric) ? "Short-Answer" : "Multiple-Choice"%></td>
    <td class="questionDelCol"><% if ((pollRoles.Contains(pollMaster) && Model.pollClose<DateTime.Now) || (pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) && (Model.pollStart==null || Model.pollClose<DateTime.Now))
                                  {  using (Html.BeginForm("DeleteQuestion", "Question", new { questionID = question.questionID, pollID = Model.linkedPoll.pollID }, FormMethod.Post))
                                                                            { %>
                               <input type="submit" value="Delete" id="questionDel<%= question.questionID %>" class="questionDel"/> <% } %> <% } %></td>
    </tr>
         <% } %>
    <% } %>
    </table>
    <input type="hidden" id="nextTestQuestionNumber" value="<%= nextTestQuestionNumber %>" />
    <% if ((pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) && Model.pollStart == null)
       { %>
    <% using (Html.BeginForm("EditCustom", "Question", new { questionID = -1, questionNumber = nextTestQuestionNumber, pollID = Model.linkedPoll.pollID, pollName = "Test Poll" }, FormMethod.Post, new { @id = "testSAQForm", onsubmit = "return newPoll($('#testerrormessage'),'test')" }))
       { %>
    <% Html.ValidationSummary(true); %>
    <input type="submit" value="Create Question" /><span class="error" id="testerrormessage"></span>
    <% } %>
    <% } %>
    <div class="clear"></div>
    <div class="questionsContainerHeadings" id="questionsHeading"><h2>Questions</h2></div>
    <table class="questions">
    <thead>
    <tr>
    <th class="questionNumberCol"></th>
    <th class="questionCol">Question</th>
    <th class="questionTypeCol">Type</th>
    <th class="questionDelCol"></th>
    </tr>
    </thead>
    <% int nextQuestionNumber = 1;
       foreach (var question in Model.questions)
       {
           if (question.deletionTime == null)
           {
               nextQuestionNumber++; %>
    <tr id="questionRow<%= question.questionID %>" class="pollDetailsQuestionRow">
    <td><%= question.questionNumber%></td>
    <td class="questionCol"><%= Html.ActionLink(question.question.CropAndTrail(45, "..."), "Edit" + question.type, "Question", new { questionID = question.questionID, questionNumber = question.questionNumber, pollID = Model.pollID, pollName = Model.name }, null)%></td>
    <td class="questionTypeCol"><%= (question.type == dbPoll_Application.Models.QuestionType.Alphanumeric || question.type == dbPoll_Application.Models.QuestionType.Numeric) ? "Short-Answer" : "Multiple-Choice"%></td>
    <td class="questionDelCol"><% if ((pollRoles.Contains(pollMaster) && Model.pollClose<DateTime.Now) || (pollRoles.Contains(pollCreator) && (Model.pollStart==null || Model.pollClose<DateTime.Now)))
                                  { %><% using (Html.BeginForm("DeleteQuestion", "Question", new { questionID = question.questionID, pollID = Model.pollID }, FormMethod.Post))
                                                                            { %>
                               <input type="submit" value="Delete" id="questionDel<%= question.questionID %>" class="questionDel"/> <% } %> <% } %></td>
    </tr>
         <% } %>
    <% } %>
    </table>
    <input type="hidden" id="nextQuestionNumber" value="<%= nextQuestionNumber %>" />
    <% if ((pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) && Model.pollStart==null) { %>
    <% using (Html.BeginForm("EditCustom", "Question", new { questionID = -1, questionNumber = nextQuestionNumber, pollID = Model.pollID, pollName = Model.name }, FormMethod.Post, new { @id = "SAQForm", onsubmit = "return newPoll($('#questionerrormessage'),'real')" }))
       { %>
    <% Html.ValidationSummary(true); %>
    <input type="submit" value="Create Question" id="createQuestion" /><span class="error" id="questionerrormessage"></span>
    <% } %>
    <% } %>
    </div>
    <% using (Html.BeginForm("UpdatePollSettings", "Poll", null, FormMethod.Post, new { id = "form1" }))
       { %>
    <%= Html.HiddenFor(m => m.pollID)%>
    <%= Html.HiddenFor(m => m.genericUsers)%>
    <%= Html.HiddenFor(m => m.anonymous)%>
    <%= Html.HiddenFor(m => m.pollStart)%>
    <%= Html.HiddenFor(m => m.latitude)%>
    <%= Html.HiddenFor(m => m.longitude)%>
    <div id="rightColumn">
        <div id="pollLocationHeading"><h2>Poll Location</h2></div>
        <div class="right" id="map_canvas">
        </div>
        <div id="setPollLocation"><span class="error" id="locationerrormessage"></span><input type="button" id="setLocation" value="Set Location" /></div>
       <% var fontTypes = new SelectList(new String[] { "Arial", "Helvetica", "Courier" });
          var fontSizes = new SelectList(Enumerable.Range(1, 64));
       %>
       <div class="right">
        <ul class="rightColumnList" id="questionSettingsList">
           <li class="rightColumnListHeading"><h2>Font Settings</h2></li>
           <li class="rightColumnListItem"><label for="fontType">Font: </label><span><%= Html.DropDownListFor(m => m.defaultFontType, fontTypes, new { @class = "fontSettings" })%></span></li>
           <li class="rightColumnListItem"><label for="fontSize">Font Size: </label><span><%= Html.DropDownListFor(m => m.defaultFontSize, fontSizes, new { @class = "fontSettings" })%></span></li>
           <li class="rightColumnListItem"><label for="fontColour">Font Colour: </label><span id="fontColourSettingsNoPadding"><input type="text" id="showFontColour" class="fontColourSettings" style="background-color: <%= Model.defaultFontColour %>" /><%= Html.HiddenFor(m => m.defaultFontColour, new { @class = "fontColourSettings" })%></span></li>
           <li class="rightColumnListItem none" id="colourpicker">
               <table id="colours">
                   <tr><td id="colourFFFFFF" class="colour"></td><td id="colourCCCCCC" class="colour"></td><td id="colourC0C0C0" class="colour"></td><td id="colour999999" class="colour"></td><td id="colour666666" class="colour"></td><td id="colour333333" class="colour"></td><td id="colour000000" class="colour"></td></tr>
                   <tr><td id="colourFFCCCC" class="colour"></td><td id="colourFF6666" class="colour"></td><td id="colourFF0000" class="colour"></td><td id="colourCC0000" class="colour"></td><td id="colour990000" class="colour"></td><td id="colour660000" class="colour"></td><td id="colour330000" class="colour"></td></tr>
                   <tr><td id="colourFFCC99" class="colour"></td><td id="colourFF9966" class="colour"></td><td id="colourFF9900" class="colour"></td><td id="colourFF6600" class="colour"></td><td id="colourCC6600" class="colour"></td><td id="colour993300" class="colour"></td><td id="colour663300" class="colour"></td></tr>
                   <tr><td id="colourFFFF99" class="colour"></td><td id="colourFFFF66" class="colour"></td><td id="colourFFCC66" class="colour"></td><td id="colourFFCC33" class="colour"></td><td id="colourCC9933" class="colour"></td><td id="colour996633" class="colour"></td><td id="colour663333" class="colour"></td></tr>
                   <tr><td id="colourFFFFCC" class="colour"></td><td id="colourFFFF33" class="colour"></td><td id="colourFFFF00" class="colour"></td><td id="colourFFCC00" class="colour"></td><td id="colour999900" class="colour"></td><td id="colour666600" class="colour"></td><td id="colour333300" class="colour"></td></tr>
                   <tr><td id="colour99FF99" class="colour"></td><td id="colour66FF99" class="colour"></td><td id="colour33FF33" class="colour"></td><td id="colour33CC00" class="colour"></td><td id="colour009900" class="colour"></td><td id="colour006600" class="colour"></td><td id="colour003300" class="colour"></td></tr>
                   <tr><td id="colour99FFFF" class="colour"></td><td id="colour33FFFF" class="colour"></td><td id="colour66CCCC" class="colour"></td><td id="colour00CCCC" class="colour"></td><td id="colour339999" class="colour"></td><td id="colour336666" class="colour"></td><td id="colour003333" class="colour"></td></tr>
                   <tr><td id="colourCCFFFF" class="colour"></td><td id="colour66FFFF" class="colour"></td><td id="colour33CCFF" class="colour"></td><td id="colour3366FF" class="colour"></td><td id="colour3333FF" class="colour"></td><td id="colour000099" class="colour"></td><td id="colour000066" class="colour"></td></tr>
                   <tr><td id="colourCCCCFF" class="colour"></td><td id="colour9999FF" class="colour"></td><td id="colour6666CC" class="colour"></td><td id="colour6633FF" class="colour"></td><td id="colour6600CC" class="colour"></td><td id="colour333399" class="colour"></td><td id="colour330099" class="colour"></td></tr>
                   <tr><td id="colourFFCCFF" class="colour"></td><td id="colourFF99FF" class="colour"></td><td id="colourCC66CC" class="colour"></td><td id="colourCC33CC" class="colour"></td><td id="colour993399" class="colour"></td><td id="colour663366" class="colour"></td><td id="colour330033" class="colour"></td></tr>
               </table>
           </li>
       </ul>
       </div>
    <% var chartTypes = new SelectList(new String[3] { "Vertical Bar", "Horizontal Bar", "Pie" });
       var valueTypes = new SelectList(new String[2] { "Count", "Percentage" });
       String[] chartColours = Model.defaultResultsChart.colours.Split(new Char[]{','});
    %>
    <div class="right">
       <ul class="rightColumnList" id="chartSettingsList">
           <li class="rightColumnListHeading"><h2>Results Chart</h2></li>
           <li class="rightColumnListItem"><%= Html.HiddenFor(m => m.defaultResultsChart.chartID)%><label for="chartType">Chart Type: </label><span><%= Html.DropDownListFor(m => m.defaultResultsChart.chartType, chartTypes, new { @class = "fontSettings" })%></span></li>
           <li class="rightColumnListItem"><label for="chartValue">Chart Value: </label><span><%= Html.DropDownListFor(m => m.defaultResultsChart.valueType, valueTypes, new { @class = "fontSettings" })%></span></li>
           <li class="rightColumnListItem"><label for="chartColours">Chart Colours: </label><span><span id="showChartColours"><span id="chartColour4" style="background-color:<%= chartColours[3] %>"></span><span id="chartColour3" style="background-color:<%= chartColours[2] %>"></span><span id="chartColour2" style="background-color:<%= chartColours[1] %>"></span><span id="chartColour1" style="background-color:<%= chartColours[0] %>"></span><%= Html.HiddenFor(m => m.defaultResultsChart.colours, new { @class = "fontColourSettings" })%></span><%= Html.HiddenFor(m => m.defaultResultsChart.active)%></li>
           <li class="rightColumnListItem none" id="chartColoursPicker">
               <table id="chartColoursTable">
                   <tr id="goyr"><td style="background-color:Green"></td><td style="background-color:Orange"></td><td style="background-color:Yellow"></td><td style="background-color:Red"></td></tr>
                   <tr id="rgoy"><td style="background-color:Red"></td><td style="background-color:Green"></td><td style="background-color:Orange"></td><td style="background-color:Yellow"></td></tr>
                   <tr id="oygr"><td style="background-color:Orange"></td><td style="background-color:Yellow"></td><td style="background-color:Green"></td><td style="background-color:Red"></td></tr>
               </table>
           </li>
       </ul>
    </div>
    <div class="right">
       <ul class="rightColumnList" id="objectSettingsList">
           <li class="rightColumnListHeading"><h2>Question Objects</h2></li>
           <li class="rightColumnListItem">Countdown Timer
                <span>
                   <%= Html.HiddenFor(m => m.defaultCountdownTimer.objectID)%>
                   <%= Html.TextBoxFor(m => m.defaultCountdownTimer.countdownAmount, new { @class = "countdownAmount" })%>
                   <%= Html.CheckBoxFor(m => m.defaultCountdownTimer.active)%>
                </span></li>
           <li class="rightColumnListItem">Response Counter
                <span>
                   <%= Html.HiddenFor(m => m.defaultResponseCounter.objectID)%>
                   <%= Html.CheckBoxFor(m => m.defaultResponseCounter.active)%>
                </span></li>
           <li class="rightColumnListItem">Correct Answer Indicator
                <span>
                   <%= Html.HiddenFor(m => m.defaultCorrectAnswerIndicator.objectID)%>
                   <%= Html.CheckBoxFor(m => m.defaultCorrectAnswerIndicator.active)%>
                </span></li>
           <li class="rightColumnListItem">Response Grid
                <span>
                <%= Html.HiddenFor(m => m.defaultResponseGrid.objectID)%>
                <%= Html.DropDownListFor(m => m.defaultResponseGrid.show, new SelectList(new String[2]{"Name", "DeviceID"})) %>
                <%= Html.CheckBoxFor(m => m.defaultResponseGrid.active)%>
                </span></li>
       </ul>
    </div>
    <div class="right">
        <span class="pollClose"><label for="pollClose">Poll Close:</label><input id="pollClose" class="datepicker" type="text" value="<%= Model.pollClose.ToString("dd/MM/yyyy hh:mm tt") %>" name="pollClose" readonly="readonly" /></span>
        <span><input type="button" value="Save Poll" id="updatePoll" /></span>
        <span id="errorMessage"></span>
    </div>
    </div><%} %>

    <div class="clear"></div>
    <div id="setPollLocationDialogContainer" class="none">
        <div id="setPollLocationDialog">
            <div id="setPollLocationHeading">Set Location</div>
            <div id="setPollLocationContent">
                <table>
                    <tr><td><label for="setLatitude">Latitude:</label></td><td><input type="text" id="setLatitude" name="setLatitude" /></td></tr>
                    <tr><td><label for="setLongitude">Longitude:</label></td><td><input type="text" id="setLongitude" name="setLongitude" /></td></tr>
                </table>
                <div id="setPollLocationButtons"><input type="button" id="setPollLocationOK" value="OK" /><input type="button" id="setPollLocationCancel" value="Cancel" /><span id="setPollLocationErrorMessage"></span></div>
            </div>
        </div>
    </div>
    </div>
    <script type="text/javascript" >
        <%            
            if  (!(pollRoles.Contains(pollCreator) || pollRoles.Contains(pollAdministrator)) || Model.pollStart!=null) 
            {%>  document.getElementById("name").readOnly=true;
        <%  } %>
        </script>
</asp:Content>
